"""
编写人: 何海舟
组名: 第六组小可爱
购物车表现层和操作层封装
"""
# 引包
from Page.LoginPage import LoginPage
from Common.Base import Base, open_browser

shoppingcart_url = 'http://172.16.7.125:8080/ecshop/index.php'


# 创建测试类
class Shoppingcart(Base):
    '''封装页面表现层 制作页面元素定位'''
    glove_loc = ("css selector", "img[alt='炫彩翻页保护套']")  # 首页手机壳定位器
    camera_loc = ("css selector", "img[alt='智能相机']")  # 首页智能相机定位器
    now_buy_loc = ("css selector", ".td1>a>img")  # 首页手机壳立即购买定位器
    goods_details_loc = ('class name', 'goods_style_name')  # 商品详情文本定位器
    shopping_goods_loc = ('xpath', '//tr[2]/td[1]/a[2]')  # 购物车第一个商品文本定位
    shoppingcart_loc = ('link text', '购物车')  # 首页购物车定位

    shopping_searchbox_loc = ('id', 'keyword')  # 购物车界面搜索框

    clothing_loc = ('link text', '服装')  # 服装定位器

    shopping_card_accounts = ("css selector", ".inputBg")  # 购物车数量输入框

    delete_loc = ('link text', '删除')  # 删除按钮
    clear_shopping_loc = ('xpath', '//input[@value="清空购物车"]')  # 清空购物车
    updata_shopping_loc = ('xpath', '//input[@value="更新购物车"]')  # 更新购物车
    updata_failure_loc = ('link text', '返回上一页')  # 更新失败 返回上一页
    continue_shopping_loc = ('xpath', '//a/img[@alt="continue"]')  # 继续购物
    settlement_loc = ('xpath', '//a/img[@alt="checkout"]')  # 去结算

    def click_glove(self):
        '''
        点击首页手机套链接
        :return:
        '''
        self.click(self.glove_loc)

    def click_camera(self):
        '''
        点击首页智能相机链接
        :return:
        '''
        self.click(self.camera_loc)

    def now_buy(self):
        '''
        首页选择商品立即购买
        :return:
        '''
        self.click(self.now_buy_loc)


    def cilck_shoppingcart(self):
        '''
        首页界面点击购物车
        :return:
        '''
        self.click(locator=self.shoppingcart_loc)

    def input_shopping_searchbox(self, text):
        '''
        购物车搜索框输入
        :return:
        '''
        self.send_keys(text=text, locator=self.shopping_searchbox_loc)

    def click_clothing(self):
        '''
        服装定位器
        :return:
        '''
        self.click(self.clothing_loc)

    def input_shopping_count(self, text):
        '''
        购物车购物数量
        :return:
        '''
        self.send_keys(text, self.shopping_card_accounts)

    def delete_shopping(self):
        '''
        删除购物车中商品
        :return:
        '''
        self.click(self.delete_loc)

    def continue_shopping(self):
        '''
        继续购物
        :return:
        '''
        self.click(self.continue_shopping_loc)

    def clear_shopping(self):
        '''
        清空购物车
        :return:
        '''
        self.click(self.clear_shopping_loc)

    def updata_shopping(self):
        '''
        更新购物车
        :return:
        '''
        self.click(self.updata_shopping_loc)

    def updata_failure(self):
        '''
        更新失败 返回上一页
        :return:
        '''
        self.click(self.updata_failure_loc)

    def settlement(self):
        '''
        去结算
        :return:
        '''
        self.click(self.settlement_loc)

    def get_goods_value(self):
        '''
        获取指定商品value值
        :return:
        '''
        return self.get_element_value(self.shopping_card_accounts)

    def get_goods_details_text(self):
        '''
        获取指定商品详情文本值
        :return:
        '''
        return self.get_text(self.goods_details_loc)

    def get_shopping_goods_text(self):
        '''
        获取购物车第一个商品文本值
        :return:
        '''
        return self.get_text(self.shopping_goods_loc)

    def delete_goods_accept(self):
        '''
        获取弹窗文本 点击确定
        :return:
        '''
        return self.get_alert_accept_text()


if __name__ == '__main__':
    from time import sleep

    driver = open_browser()
    sc = Shoppingcart(driver)
    sc.open_url(shoppingcart_url)
    sc.click_glove()  # 点击保护套
    sleep(2)
    sc.now_buy()  # 点击立即购买
    sleep(2)
    sc.input_shopping_count('2')
    sleep(3)
    # sc.click_shopping_count()  # 点击数量
    # sc.input_shopping_count('5')

    # sc.input_shopping_searchbox('炫彩翻页保护套')
    # sleep(2)
    # sc.continue_shopping()  # 继续购物
    # sleep(2)
    # sc.clear_shopping()  # 清空购物车
    # sleep(2)

    # sc.delete_shopping()  # 删除商品
    # alert = sc.driver.switch_to.alert
    # sleep(2)
    # alert.dismiss()  # 点击取消
    # sc.delete_shopping()  # 删除商品
    # alert = sc.driver.switch_to.alert
    # sleep(2)
    # alert.accept()  # 点击确定

    # sc.delete_shopping()
    # sleep(2)
    # print(sc.get_alert_dismiss())
    # sc.delete_shopping()
    # sleep(2)
    # sc.get_alert_accept_text()

    sc.updata_shopping()  # 点击更新购物车
    sleep(3)

    sc.close_browser()
